<?php
namespace Admin\Controller;
class SettingController extends CommonController {
    public function _initialize() {
        parent::_initialize();
    }
    public function index() {
        $type = I('type', 'index', 'trim');
        $name = I('name','site','trim');
        $id   = I('get.id',0,'int');
        $site_id = $id>0 ? $id : $this->siteId;
        $where = array('name'=>$name,'site_id'=>$site_id);
        $list = M('setting')->where($where)->find();
        $info = json_decode($list['data'],true);
        $urlsetting = require './App/Home/Conf/site.php';
        $this->assign('url',$urlsetting);
        $this->assign("info",$info);
        $this->assign('site_id',$site_id);
        $this->display($type);
    }
	//图片水印
	public function watermark(){
        $config_file = './App/Admin/Conf/water.php';
		if(IS_POST){
        //$config = require $config_file;
		$new_config = array(

                                    'WATER_FUNTION'=>I('WATER_FUNTION','intval'),	//是否开启水印功能
                                    'WATER_FILE_TYPE'=>I('WATER_FILE_TYPE','intval'),	//选择水印的类型
                                    'WATER_IMAGE' => I('WATER_IMAGE'),	//图片水印路径
                                    'WATER_POS'=> I('WATER_POS','intval'),//水印位置
                                    'WATER_ALPHA'=> I('WATER_ALPHA','intval'),	//水印透明度
                                    'WATER_COMPRESSION'=>80,	//JPEG图片压缩比
                                    'WATER_TEXT'=> I('WATER_TEXT','trim'),	//水印文字
                                    'WATER_ANGLE'=> I('WATER_ANGLE','intval'),	//水印文字旋转角度
                                    'WATER_FONTSIZE'=> I('WATER_FONTSIZE','intval'),	//字体大小
                                    'WATER_FONTCOLOR'=> I('WATER_FONTCOLOR'),	//水印字体颜色
                                    'WATER_FONTFILE'=>'./data/water/font.ttf',	//水印字体
                                    'WATER_CHARSET'=>'UTF-8',	//水印文字编码
        );
		   if (update_config($new_config, $config_file,$this->siteId)) {
                $this->success('修改成功');
            } else {
                $this->error(L('修改失败'));
            }
		}else{
            $info = require $config_file;
            $this->assign('info',$info[$this->siteId]);
            $this->display();
		}
	}

    public function edit() {
        if(IS_POST){
            $config = M('setting');
            $info = $_POST['info'];
            $site_id = I('post.site_id',0,'int');
            if(empty($site_id)){
                $site_id = $this->siteId;
            }
            $name = array_keys($info);
            $values = array_values($info);
            $name = $name[0];

            $value = urldecode(json_encode(array_map('urlencode',$values[0])));
            $weijintai = I('post.weijintai',0,'int');
            $config_file = './App/Home/Conf/site.php';
            $ws = $_POST['w'];
            if($weijintai == 1){
                $new_config = array(
                    'URL_MODEL' => $ws['moshi'] ? $ws['moshi'] : 2,//URL模式
                    'URL_PATHINFO_DEPR' => $ws['fenge'],
                    'URL_HTML_SUFFIX' => $ws['houzhui'],
                );

            }else{
                $new_config = array(
                    'URL_MODEL' => 0,//URL模式
                   // 'URL_PATHINFO_DEPR' => NULL,
                   // 'URL_HTML_SUFFIX' => NULL,
                );
            }
            $new_config['SCRIPTCODE'] = addslashes($ws['countcode']);
            $new_config['REPLACE_NUMS'] = $ws['replacenum'];
            $flag = update_config($new_config, $config_file,0);
            $where = array('name'=>$name,'site_id'=>$site_id);
            if($config->where($where)->find()){
                if($config->where($where)->setField('data', $value) || $flag){
                    $this->success('修改成功！');
                }else{
                    $this->error('修改失败！');
                }
            }else{
               if($config->add(array('name'=>$name,'data'=>$value)) || $flag){
                   $this->success('保存成功！');
               }else{
                   $this->error('保存失败！');
               }
            }
        }
    }

}